﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace LS.Cores
{
    public class PlatformFactory
    {
        private static Lazy<PlatformFactory> _instance = new Lazy<PlatformFactory>(() => new PlatformFactory());

        public static PlatformFactory Instance
        {
            get
            {
                return _instance.Value;
            }
        }

        /// <summary>
        /// 程序集信息
        /// </summary>
        private Assembly _assembly;

        private PlatformFactory()
        {
            _assembly = Assembly.Load("LS.Cores");
        }

        /// <summary>
        /// 创建实例
        /// </summary>
        /// <param name="platformKey"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private object CreateInstance(string eipKey, params object[] args)
        {
            string serverName = eipKey + "service";
            var list = _assembly.GetTypes();
            foreach (var item in list)
            {
                if (item.Name.ToLower()== serverName.ToLower())
                {
                    return Activator.CreateInstance(item, args); ;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取平台实现类对象
        /// </summary>
        /// <param name="platformKey"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public IExcelOperate GetExcelService(string platformKey, params object[] args)
        {
            object obj = CreateInstance(platformKey, args);
            if (null == obj)
            {
                return null;
            }
            else
            {
                return (IExcelOperate)obj;
            }
        }
    }
}
